import axios from 'axios'
import API from '../../api'
import { handImgPath } from '../../utils/commonFn.js'

//请求定位
export function getAddess(lat,lon){
	return new Promise((resolve,reject)=>{
		axios.get(API.ADRESS_API,{
			params:{
				latitude:lat,
				longitude:lon
			}
		})
		.then((response)=>{
			resolve(response);
		})
		.catch((error)=>{
			reject(error);
		})
	})
}

//轮播图数据请求
export function getEentries(lat,lon,templates){
	return new Promise((resolve,reject)=>{
		axios.get(API.ENTRIES_API,{
			params:{
				latitude:lat,
				longitude:lon,
				templates:templates,
				terminal:'h5'
			}
		})
		.then((response)=>{
			//过滤需要的字段
			let oneData = response.data[0].entries.map((item)=>{
				return {
					name: item.name,
					id: item.id,
					img: handImgPath(item.image_hash,90,90)
				}
			})
			//每3个数据为一组
			let twoData = [];
			while(oneData.length > 0){
				twoData.push(oneData.splice(0,3))
			}
			resolve(twoData);
		})
		.catch((error)=>{
			reject(error);
		})
	})
}